﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace BawangMemo.BackendModels
{
    internal class Helper
    {
        private static Random random = new Random();
        private static XmlWriterSettings xmlSetting;

        static Helper()
        {
            xmlSetting = new XmlWriterSettings();
            xmlSetting.Indent = true;
            xmlSetting.NewLineOnAttributes = true;
        }

        public delegate void SetProfFunc(WordWrapper ww, int proficiency);

        internal static void UserResponseStatic(WordWrapper ww, int proficiency, SetProfFunc func)
        {
            WordInfo wi = ww.WordInfo;
            if (func == null)
            {
                wi.TotalProficiency = (wi.TotalMemCount * wi.TotalProficiency + (10.0 * proficiency) / 3)
                    / (wi.TotalMemCount + 1);
            }
            else
            {
                func(ww, proficiency);
            }
            WordRepository.SaveWordInfo(ww.Course.WordsInfoPath, ww.WordInfo);
        }

        internal static int[] GenerateOrder(WordOrder order, int n)
        {
            int[] array = new int[n];
            for (int i = 0; i < n; i++) array[i] = n - i - 1;
            if (order.Equals(WordOrder.Randomized))
            {
                for (int i = n - 1; i > 0; i--)
                {
                    Swap(ref array[i], ref array[random.Next(i + 1)]);
                }
            }
            else if (order.Equals(WordOrder.Sequential))
            {
                for (int i = 0; i < n / 2; i++)
                {
                    Swap(ref array[i], ref array[n - i - 1]);
                }
            }
            return array;
        }

        private static void Swap(ref int a, ref int b)
        {
            int c = a;
            a = b;
            b = c;
        }

        internal static void SaveXML(System.Xml.Linq.XDocument doc, string path)
        {
            using (XmlWriter writer = XmlWriter.Create(path, xmlSetting))
            {
                doc.WriteTo(writer);
                writer.Flush();
                writer.Close();
            }
        }

        internal static string GetAttributeValue(System.Xml.Linq.XElement elem, string attrName)
        {
            System.Xml.Linq.XAttribute xa = elem.Attribute(attrName);
            return xa != null ? xa.Value : String.Empty;
        }

        internal static string quote(string tag)
        {
            return tag.Replace("'", "\\'");
        }

        private static string[] specialChars = { "&quot;", "&amp;nbsp;", "&lt;", "&gt;", "&amp;gt;", "&amp;lt;", "&nbsp;", "\\n" };

        private static string[] renderChars = { "\"", "", "<", ">", ">", "<", "", "\n" };

        internal static string replaceSpecialChars(string s)
        {
            for (int i = 0; i < specialChars.Length; i++)
            {
                s = s.Replace(specialChars[i], renderChars[i]);
            }
            return s.Trim();
        }

        internal static bool IsNotNullOrEmpty(string s)
        {
            return s != null && !s.Equals(String.Empty);
        }

    }
}
